package com.li.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.shop.domain.User;
import com.li.shop.domain.UserExample;
import com.li.shop.mapper.UserMapper;
import com.li.shop.service.UserService;
import com.li.shop.utils.SearchUserRequest;
import com.li.shop.utils.SearchUserResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //通过查找用户名获取用户号(即主键)
    public String findPriKeyByUserName(String user_name){
        String u_id = userMapper.findByUserName(user_name).getuId();
        return u_id;
    }

    public Integer findUser(String userName){
        if(userMapper.findUser(userName)==null){            //每次generate后会消失，导入创建的时候会自动生成返回值是Boolean的方法，报错，改成String类型就OK
            return 0;                           //如果没有查找到对应的主键，则返回0
        }else{
            return 1;
        }
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    //    传参传id，通过方法查找
    @Override
    public void delete(String user_name){
        userMapper.deleteByPrimaryKey(findPriKeyByUserName(user_name));
    }


    @Override
    public User login(String user_name, String password) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserNameEqualTo(user_name).andUserPasswordEqualTo(password);
        List<User> users=userMapper.selectByExample(userExample);
        if(users.isEmpty()){
            return null;
        }
        return users.get(0);
    }

    @Override
    public User get(String user_name) {
        return userMapper.findByUserName(user_name);
    }

    @Override
    public List<User> list() {
        UserExample example = new UserExample();
        example.setOrderByClause("id desc");
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    @Override
    public User get(String user_name, String password) {
        UserExample example = new UserExample();
        example.createCriteria().andUserNameEqualTo(user_name).andUserPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(example);
        if(users.isEmpty()){
            return null;
        }
        return users.get(0);
    }


    @Override
    public boolean exist(String user_name) {
        UserExample example = new UserExample();
        example.createCriteria().andUserNameEqualTo(user_name);
        List<User> users = userMapper.selectByExample(example);
        if(users.isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public Integer insert(User user) {
        return userMapper.insert(user);
    }



    //王志文
    @Override
    //public List<User> listUser(Integer pageSize,Integer pageNum) {
    public SearchUserResponse listUser(Integer pageSize, Integer pageNum, String keyword) {
        //PageHelper.startPage(1,10);
        PageHelper.startPage(pageNum,pageSize);
        UserExample example =new UserExample();
        if (StringUtils.isNotBlank(keyword)) {
            example.createCriteria().andUserNameLike("%"+keyword+"%");
        }
        List<User> users= userMapper.selectByExample(example);
        return  new SearchUserResponse(((Page)users).getTotal(),users);
        //return UserMapper.selectByExample(new UserExample());//分页后返回的page对象，page中有一个list对象
    }


    @Override
    public int deleteUserByPrimaryKey(String id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User findUserByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> searchUsers(SearchUserRequest request) {
        UserExample ex = new UserExample();
//        ex.createCriteria().andUserIdEqualTo(request.getId());
//        ex.or().andLastNameLike("%" + request.getName() + "%");
        return userMapper.selectByExample(ex);
    }

    @Override
    public int saveUser( User user) {
        System.out.println(user.getuId()+"userid");
        if(StringUtils.isNotBlank( user.getuId()) ){
            //if( user.getuId()!=null ){
            //把domain类中的uId类型转化为String
            return userMapper.updateByPrimaryKey(user);
        } else {
            return userMapper.insert(user);

        }
    }

    @Override
    public User searchById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public int updatemessage(User user) {
        return userMapper.updatemessgae(user);
    }
    @Override//w
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override//w
    public int updateUserById(User user) {
//        return userMapper.updateAdrById(uid,adr);

        return userMapper.updateByPrimaryKeySelective(user);
     }

}
